import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBack:false,
    startDate:'',
    endDate:'',
    date_popup_show:false,
    search_state:'',
    search_mealType:'',
    commit_time:'',
    keyword:'',
    stateOption:[],
    timeOption:[],
    time_popup_show:false,
    popup_show:false,
    list:[],
    page:1,
    nowType:'mealType',
    actionShow:false,
    actions:[
      {
        name: '取消报餐',
      },
      // {
      //   name: '删除',
      //   color: '#ee0a24'
      // },
    ],
    targetId:'',
  },

  /**菜单 */
  setting(e){
    this.setData({ 
      actionShow: true,
      targetId:e.currentTarget.dataset.id
    });
  },
  onActionClose() {
    this.setData({ actionShow: false });
  },
  onActionSelect(event) {
    let that = this;
    if(event.detail.name=='取消报餐'){
      wx.showModal({
        title: '提示',
        content: '是否确定取消此报餐？',
        success (res) {
          if (res.confirm) {
            api.curlPost('?cla=canteenApp&fun=cancelReport',{id:that.data.targetId}).then((res)=>{
              that.setData({
                targetId:'',
              });
              that.deleteData(that.data.targetId)
            })
          } 
        }
      })
    }
   
  },
  /**开始时间选择 */
  bindStartDateChange(e){
    this.setData({
      startDate: e.detail.value
    })
  },
  /**结束时间选择 */
  bindEndDateChange(e){
    this.setData({
      endDate: e.detail.value
    })
  },
  /**自定义时间选择确认 */
  dateSearch(){
    this.setData({
      commit_time:'',
      date_popup_show: false
    })
    this.requestData();
  },

/**显示状态选项搜索 */
showPopup(e) {
  let that = this;
  api.curlPost('?cla=canteenApp&fun=replaceReportSearch').then((res)=>{
    if(e.currentTarget.dataset.type =='mealType'){
      that.setData({
        nowType:e.currentTarget.dataset.type,
        popup_show:true,
        stateOption:res.data.mealOption
      })
    }else{
      that.setData({
        nowType:e.currentTarget.dataset.type,
        popup_show:true,
        stateOption:res.data.stateOption
      })
    }
  })
},
showTimePopup(){
  let that = this;
  api.curlPost('?cla=canteenApp&fun=replaceReportSearch').then((res)=>{
    that.setData({
      time_popup_show:true,
      timeOption:res.data.timeOption
    })
  })
},
  /**选项弹出层关闭 */
  onStateCancel(e){
    this.setData({
      popup_show:false
    })
  },
  /**搜索选项切换 */
  onStateConfirm(event){
    const { picker, value, index } = event.detail;
    if(this.data.nowType=='mealType'){
      this.setData({
        search_mealType:value,
        popup_show:false
      })
    }else{
      this.setData({
        search_state:value,
        popup_show:false
      })
    }
   
    this.requestData();
  },
  /**时间选项弹出层关闭 */
  onTimeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  /**时间搜索选项切换 */
  onTimeConfirm(event){
    const { picker, value, index } = event.detail;
    if(value=='自定义'){
      this.setData({
        date_popup_show:true,
        time_popup_show:false
      })
    }else{
      this.setData({
        commit_time:`${value}`,
        startDate:'',
        endDate:'',
        time_popup_show:false
      })
      this.requestData();
    }
  },
  /**搜索 */
  onSearch(e){
    this.requestData()
  },
  /**搜索文字变动 */
  searchChange(e){
    this.setData({
      keyword: e.detail,
    });
  },
  

  /**删除数据 */
  deleteData(id){
    let list = this.data.list;
    let newList = list.filter(item => item.id !=id );
    this.setData({
      list:newList
    })
  },

  /**
   * 请求数据
   */
  requestData:function(page=1){
    let that = this;
    let url = '?cla=canteenApp&fun=replaceReportMeal&page='+page
    let para = {
      keyword:this.data.keyword,
      workFlow:this.data.search_state,
      mealType:this.data.search_mealType,
      commit_time:this.data.commit_time,
      startDate:this.data.startDate,
      endDate:this.data.endDate
    }
    api.curlPost(url,para).then((res)=>{
      if(!api.isBlank(res.data.result) || page ==1){
        let arr = page==1?res.data.result:that.data.list.concat(res.data.result);
        that.setData({
          list:arr,
          page:Number(res.data.page.pageNow) +1
        })
        wx.stopPullDownRefresh()
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  edit(e){
    wx.navigateTo({
      url: '../mealAskEdit/mealAskEdit',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.requestData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestData(this.data.page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})